import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name:'index',
    component: () => import('../views/Home.vue'),
    children:[
      {
        path:'users',
        name:'users',
        component: () => import('../views/users.vue'),
      },
      {
        path:'roles',
        name:'roles',
        component: () => import('../views/roles.vue'),
      },
      
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/Home',
    name: 'Home',
    component: () => import('../views/Home.vue')
  }
]


const router = new VueRouter({
  routes
})
//导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/Home' || to.path == '/') {
    if (!localStorage.getItem('userinfo')) {
      next('/Login')
    }
  }
 
  //如果已经登陆了，从哪里来回哪里去。登录页面只能在未登录的情况下才能访问
  if(to.path == '/Login'){
    if(localStorage.getItem('userinfo')){
      next(from)
    }
  }
  next()
})

export default router
